package com.zhangjinghao.controller;

import com.zhangjinghao.entity.City;
import com.zhangjinghao.entity.Food;
import com.zhangjinghao.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("city")
public class CityController {

    @Autowired
    private CityService cityService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 获取城市列表
     * @param pid
     * @return
     */
    @RequestMapping("getCitys")
    @ResponseBody
    public List<City> getCitys(Integer pid){
        List<City> cities = null;
        if (redisTemplate.hasKey("citys-"+pid.toString())){
            cities = (List<City>) redisTemplate.opsForValue().get("citys-"+pid.toString());
        }else{
            cities = cityService.selectCity(pid);
            redisTemplate.opsForValue().set("citys-"+pid.toString(),cities,new Random().nextInt(1000)+1000, TimeUnit.MINUTES);
        }
        return cities;
    }

}
